package com.ssy.lingxi.settle.accounts.controller;


import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.dto.request.*;
import com.ssy.lingxi.dto.response.InvoiceMessageResponse;
import com.ssy.lingxi.settle.accounts.service.InvoiceMessageService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;


/**
 *@menu  结算能力-结算规则配置-发票信息管理
 * @author wrc
 * @date 2020/8/5
 */
@RestController
@RequestMapping("/settle/accounts")
public class InvoiceMessageController extends BaseController {


    @Resource
    InvoiceMessageService invoiceMessageService;


    /**
     * 结算能力-结算规则配置-发票信息列表查询
     * @author wrc
     * @date 2020/8/25
     */
    @GetMapping("/invoice/message/list")
    public Wrapper<List<InvoiceMessageResponse>> invoiceMessageList(HttpServletRequest request) {
        UserLoginCacheDTO sysUser = this.getSysUser(request);
        return invoiceMessageService.invoiceMessageList(sysUser);
    }


    /**
     * 结算能力-结算规则配置-发票信息详情查询
     * @author wrc
     * @date 2020/8/25
     */
    @GetMapping("/invoice/message/details")
    public Wrapper<InvoiceMessageResponse> invoiceMessageDetails(InvoiceMessageDeleteRequest request) {
        return invoiceMessageService.invoiceMessageDetails(request);
    }



    /**
     * 结算能力-结算规则配置-发票信息详情查询内部调用
     * @author wrc
     * @date 2020/8/25
     */
    @GetMapping("/invoice/message/details/inside")
    public Wrapper<InvoiceMessageResponse> invoiceMessageDetailsInside(@RequestParam("id") Long id) {
        InvoiceMessageDeleteRequest request=new InvoiceMessageDeleteRequest();
        request.setId(id);
        return invoiceMessageService.invoiceMessageDetails(request);
    }
    /**
     * 结算能力-结算规则配置-发票信息详情查询内部调用
     * @author wrc
     * @date 2020/8/25
     */
    @PostMapping("/invoice/message/member/list")
    public Wrapper<List<InvoiceMessageResponse>> getMemberInvoiceMessageList(@RequestBody @Valid InvoiceMessageListRequest request) {
        return invoiceMessageService.getMemberInvoiceMessageList(request);
    }

    /**
     * 结算能力 结算规则配置 发票信息添加
     * @author wrc
     * @date 2020/8/25
     */
    @PostMapping("/invoice/message/add")
    public Wrapper<Void> invoiceMessageAdd(@Valid @RequestBody InvoiceMessageAddRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return invoiceMessageService.invoiceMessageAdd(request,sysUser);
    }


    /**
     * 结算能力 结算规则配置 发票信息修改
     * @author wrc
     * @date 2020/8/25
     */
    @PostMapping("/invoice/message/update")
    public Wrapper<Void> invoiceMessageUpdate(@Valid @RequestBody InvoiceMessageUpdateRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return invoiceMessageService.invoiceMessageUpdate(request,sysUser);
    }

    /**
     * 结算能力 结算规则配置 发票信息删除
     * @author wrc
     * @date 2020/8/25
     */
    @PostMapping("/invoice/message/delete")
    public Wrapper<Void> invoiceMessageDelete(@Valid @RequestBody InvoiceMessageDeleteRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return invoiceMessageService.invoiceMessageDelete(request,sysUser);
    }

    /**************************************代客下单操作发票****************************************/
    //构建代理对象
    private UserLoginCacheDTO buildAgentUserLoginCacheDTO(Long memberId, Long roleId){
        UserLoginCacheDTO agent=new UserLoginCacheDTO();
        agent.setMemberId(memberId);
        agent.setMemberRoleId(roleId);
        return agent;
    }

    /**
     * 【代客】结算能力-结算规则配置-发票信息列表查询
     * @author wrc
     * @date 2020/8/25
     */
    @GetMapping("/agent/invoice/message/list")
    public Wrapper<List<InvoiceMessageResponse>> agentInvoiceMessageList(HttpServletRequest request,@RequestParam Long subMemberId,@RequestParam Long subRoleId) {
        this.getSysUser(request);
        UserLoginCacheDTO agent= buildAgentUserLoginCacheDTO(subMemberId,subRoleId);
        return invoiceMessageService.invoiceMessageList(agent);
    }


    /**
     * 【代客】结算能力 结算规则配置 发票信息添加
     * @author wrc
     * @date 2020/8/25
     */
    @PostMapping("/agent/invoice/message/add")
    public Wrapper<Void> agentInvoiceMessageAdd(@Valid @RequestBody InvoiceMessageAgentAddRequest request, HttpServletRequest httpServletRequest) {
        this.getSysUser(httpServletRequest);
        UserLoginCacheDTO agent= buildAgentUserLoginCacheDTO(request.getSubMemberId(),request.getSubRoleId());
        return invoiceMessageService.invoiceMessageAdd(request,agent);
    }

    /**
     * 【代客】结算能力 结算规则配置 发票信息修改
     * @author wrc
     * @date 2020/8/25
     */
    @PostMapping("/agent/invoice/message/update")
    public Wrapper<Void> agentInvoiceMessageUpdate(@Valid @RequestBody InvoiceMessageAgentUpdateRequest request, HttpServletRequest httpServletRequest) {
        this.getSysUser(httpServletRequest);
        UserLoginCacheDTO agent= buildAgentUserLoginCacheDTO(request.getSubMemberId(),request.getSubRoleId());
        return invoiceMessageService.invoiceMessageUpdate(request,agent);
    }

    /**
     * 【代客】结算能力 结算规则配置 发票信息删除
     * @author wrc
     * @date 2020/8/25
     */
    @PostMapping("/agent/invoice/message/delete")
    public Wrapper<Void> agentInvoiceMessageDelete(@Valid @RequestBody InvoiceMessageAgentDeleteRequest request, HttpServletRequest httpServletRequest) {
        this.getSysUser(httpServletRequest);
        UserLoginCacheDTO agent= buildAgentUserLoginCacheDTO(request.getSubMemberId(),request.getSubRoleId());
        return invoiceMessageService.invoiceMessageDelete(request,agent);
    }

}
